package com.yz.crm.passport.action;
import com.yz.crm.passport.constant.PassportConst;
import com.yz.crm.passport.util.ImageCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 发送验证码
 * Created by DOVE on 2015/7/10.
 */
@Controller
@RequestMapping("/passport")
public class ImgCodeAction {
    private static final Log log = LogFactory.getLog(ImgCodeAction.class);

    @RequestMapping("/checkcode")
    public void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = ImageCodeUtils.generateVerifyCode(4);
        //存入会话session
        HttpSession session = request.getSession(true);
        session.setAttribute(PassportConst.SESSION_KEY_CHECK_CODE, verifyCode.toLowerCase());
        //生成图片
        int w = 102, h = 36;
        ImageCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }
}
